package pers.spj.image.analysis.ui

import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.components.JBScrollPane
import pers.spj.image.analysis.iface.OnCancelListener
import pers.spj.image.analysis.iface.OnConfirmListener
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.Font
import javax.swing.Box
import javax.swing.BoxLayout
import javax.swing.JCheckBox
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.border.EmptyBorder
import javax.swing.event.ChangeListener

/**
 * @author supeijin
 */
internal class AnalysisResultEntry : JPanel {
    private val unusedImages: List<VirtualFile>;
    private var mOnCancelListener: OnCancelListener? = null
    private var mOnConfirmListener: OnConfirmListener<MutableList<VirtualFile>>? = null
    private var selectedFiles: MutableList<VirtualFile> = ArrayList<VirtualFile>();
    private var allCheckBoxs: MutableList<JCheckBox> = ArrayList<JCheckBox>();

    constructor(unusedImages: List<VirtualFile>) : super() {
        this.unusedImages = unusedImages;
        this.preferredSize = Dimension(600, 600)
        this.layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
        this.add(getHeader(),BorderLayout.PAGE_START);
        this.add(getJBScrollPane(unusedImages), BorderLayout.CENTER);
        this.add(getFooter(),BorderLayout.PAGE_END);
    }

    private fun getFooter(): EntryFooter {
        val entryFooter = EntryFooter("Cancel", "Delete")
        entryFooter.setOnCancelListener(object : OnCancelListener {
            override fun onCancel() {
                mOnCancelListener?.onCancel();
            }
        })
        entryFooter.setOnConfirmListener(object : OnConfirmListener<String> {
            override fun onConfirm(t: String) {
                mOnConfirmListener?.onConfirm(selectedFiles);
            }
        })
        return entryFooter
    }

    private fun getHeader(): JPanel {
        val mHeader = JPanel()
        mHeader.layout=BoxLayout(mHeader,BoxLayout.LINE_AXIS);
        mHeader.preferredSize = Dimension(600, 60)
        mHeader.maximumSize= Dimension(600,60);
        mHeader.border = EmptyBorder(10, 10, 10, 10)
        val jCheckBox = JCheckBox();
        jCheckBox.preferredSize = Dimension(32, 32);
        jCheckBox.isSelected = false
        jCheckBox.addChangeListener(ChangeListener {
            updateAllSelectedState(jCheckBox.isSelected);
        })
        mHeader.add(jCheckBox);
        mHeader.add(Box.createRigidArea(Dimension(10, 30)));
        val jLabel = JLabel()
        jLabel.preferredSize = Dimension(200, 32)
        jLabel.font = Font(jLabel.font.fontName, Font.BOLD, jLabel.font.size)
        jLabel.text = "image name"
        mHeader.add(jLabel)
        mHeader.add(Box.createHorizontalGlue())
        return mHeader
    }

    private fun getJBScrollPane(virtualFiles: List<VirtualFile>): JBScrollPane {
        val emptyBorder = EmptyBorder(10, 10, 10, 10)
        val scrollContent = JPanel()
        scrollContent.border = emptyBorder
        scrollContent.layout = BoxLayout(scrollContent, BoxLayout.PAGE_AXIS)
        for (virtualFile in virtualFiles) {
            val jPanel = JPanel()
            jPanel.layout=BoxLayout(jPanel,BoxLayout.LINE_AXIS);
            val jCheckBox = JCheckBox();
            allCheckBoxs.add(jCheckBox);
            jCheckBox.preferredSize = Dimension(32, 32);
            jCheckBox.isSelected = false
            jCheckBox.addChangeListener(ChangeListener {
                updateSelectedState(virtualFile, jCheckBox.isSelected);
            })
            jPanel.add(jCheckBox);
            val jLabel = JLabel()
            jLabel.text = virtualFile.name
            jLabel.preferredSize = Dimension(360, 32)
            jLabel.border = emptyBorder
            jLabel.font = Font(jLabel.font.fontName, Font.BOLD, jLabel.font.size)
            jPanel.add(jLabel)
            jPanel.add(Box.createHorizontalGlue())
            scrollContent.add(jPanel)
        }
        return JBScrollPane(scrollContent)
    }

    private fun updateSelectedState(virtualFile: VirtualFile, add: Boolean) {
        if (add) {
            for (file in this.selectedFiles) {
                if (file == virtualFile) {
                    return
                }
            }
            this.selectedFiles.add(virtualFile);
        } else {
            this.selectedFiles.remove(virtualFile)
        }
    }

    private fun updateAllSelectedState(selected: Boolean) {
        for (checkBox in this.allCheckBoxs) {
            checkBox.isSelected = selected;
        }
    }

    fun setOnConfirmListener(listener: OnConfirmListener<MutableList<VirtualFile>>) {
        mOnConfirmListener = listener;
    }

    fun setOnCancelListener(listener: OnCancelListener) {
        mOnCancelListener = listener;
    }
}
